/*
 * Copyright (C) 2010 Trusted Logic S.A.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/i2c.h>
#include <linux/irq.h>
#include <linux/jiffies.h>
#include <linux/uaccess.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/platform_device.h>
#include <linux/gpio.h>
#include <linux/miscdevice.h>
#include <linux/spinlock.h>
#include <linux/pn544.h>

#ifdef CONFIG_SNFC
#define MAX_BUFFER_SIZE	512
#define MAX_LLC_FRAME_SIZE 0x20
#define MAX_TRY_I2C_READ 10

#define NFC_ON_DELAY	150		/* NFC_ON_DELAY is used to ready SNFC Chip Start */
#define KSK_MORE_KERNEL2636

//#define NFC_DEBUG
#else
#define MAX_BUFFER_SIZE	256
#endif

struct pn544_dev	{
	wait_queue_head_t	read_wq;
	struct mutex		read_mutex;
	struct i2c_client	*client;
	struct miscdevice	pn544_device;
	unsigned int 		ven_gpio;
	unsigned int 		firm_gpio;
	unsigned int		irq_gpio;
	bool			irq_enabled;
	spinlock_t		irq_enabled_lock;
};

#ifdef CONFIG_SNFC
bool do_reading;

enum {
	NORMAL_MODE = 0,
	DOWNLOAD_MODE = 1,
};

static bool operation_mode = NORMAL_MODE;
#endif

static void pn544_disable_irq(struct pn544_dev *pn544_dev)
{
	unsigned long flags;

	spin_lock_irqsave(&pn544_dev->irq_enabled_lock, flags);
	if (pn544_dev->irq_enabled) {
		disable_irq_nosync(pn544_dev->client->irq);
		pn544_dev->irq_enabled = false;
	}
	spin_unlock_irqrestore(&pn544_dev->irq_enabled_lock, flags);
}

static irqreturn_t pn544_dev_irq_handler(int irq, void *dev_id)
{
	struct pn544_dev *pn544_dev = dev_id;
#ifdef CONFIG_SNFC
	if (!gpio_get_value(pn544_dev->irq_gpio))
		return IRQ_HANDLED;
#endif
	pn544_disable_irq(pn544_dev);

	/* Wake up waiting readers */
	wake_up(&pn544_dev->read_wq);
#ifdef CONFIG_SNFC
	do_reading=1;

#ifdef NFC_DEBUG
	pr_info("pn544 : call\n");
#endif
#endif
	return IRQ_HANDLED;
}

static ssize_t pn544_dev_read(struct file *filp, char __user *buf,
		size_t count, loff_t *offset)
{
	struct pn544_dev *pn544_dev = filp->private_data;
	char tmp[MAX_BUFFER_SIZE];
	int ret;
#ifdef CONFIG_SNFC
	char datasizelength = 1;
	char datasize = 0;
	char readingWatchdog = 0;
#endif

	if (count > MAX_BUFFER_SIZE)
		count = MAX_BUFFER_SIZE;

#ifdef CONFIG_SNFC
	pr_debug("%s : reading %zu bytes. irq=%s\n", __func__, count,gpio_get_value(pn544_dev->irq_gpio)?"1":"0");
#else
	pr_debug("%s : reading %zu bytes.\n", __func__, count);
#endif

#if defined(CONFIG_SNFC) && defined(NFC_DEBUG)
	pr_info("pn544 : + r\n");
#endif

	mutex_lock(&pn544_dev->read_mutex);

#ifdef CONFIG_SNFC
wait_irq :
#endif
	if(!gpio_get_value(pn544_dev->irq_gpio)) {
		if (filp->f_flags & O_NONBLOCK) {
#ifdef CONFIG_SNFC
			pr_info("%s : O_NONBLOCK\n", __func__);
#endif
			ret = -EAGAIN;
			goto fail;
		}

		pn544_dev->irq_enabled = true;
#ifdef CONFIG_SNFC
		do_reading=0;
#endif
		enable_irq(pn544_dev->client->irq);

#ifdef CONFIG_SNFC		
		ret = wait_event_interruptible(pn544_dev->read_wq,do_reading);
		/* gpio_get_value(pn544_dev->irq_gpio)); */
		/* pr_info("pn544 : h\n"); */
#else
		ret = wait_event_interruptible(pn544_dev->read_wq,
				gpio_get_value(pn544_dev->irq_gpio));
#endif
		pn544_disable_irq(pn544_dev);
		
#if defined(CONFIG_SNFC) && defined(NFC_DEBUG)
		pr_info("pn544 :   h\n");
#endif

		if (ret)
			goto fail;

	}
	/* Read data */
#ifdef CONFIG_SNFC
	if (operation_mode == NORMAL_MODE)/*Normal mode*/
	{
		ret = i2c_master_recv(pn544_dev->client, tmp, datasizelength);

		if (copy_to_user(buf, tmp, datasizelength)) {
			pr_warning("%s : failed to copy to user space\n", __func__);
			return -EFAULT;
		}

		/*If bad frame is received from pn544, we need to read again after waiting that IRQ is down*/
		if (MAX_LLC_FRAME_SIZE<tmp[0] && readingWatchdog<MAX_TRY_I2C_READ)
		{
			pr_err("pn544_dev_read: size>MAX_LLC_FRAME_SIZE received!");
			msleep(2);//sleep 2ms to wait IRQ fall down
			readingWatchdog++;
			goto wait_irq;
		}

		datasize = tmp[0];

		ret = i2c_master_recv(pn544_dev->client, tmp, datasize);

	      mutex_unlock(&pn544_dev->read_mutex);

		if (ret < 0) {
			pr_err("%s: i2c_master_recv returned %d\n", __func__, ret);
			return ret;
		}
		if (ret > datasize+1) {
			pr_err("%s: received too many bytes from i2c (%d)\n",
				__func__, ret);
			return -EIO;
		}

		if (copy_to_user(buf+1, tmp, ret)) {
			pr_err("%s : failed to copy to user space\n", __func__);
				return -EFAULT;
		}
		//add one more byte for size byte (read on first time)
		ret++;
	}
	else if (operation_mode == DOWNLOAD_MODE) /*download mode*/
	{
		ret = i2c_master_recv(pn544_dev->client, tmp, count);

		mutex_unlock(&pn544_dev->read_mutex);
	
        	if (ret < 0) {
		    pr_err("%s: i2c_master_recv returned %d\n", __func__, ret);
		    return ret;
	        }
	        if (ret > count) {
		    pr_err("%s: received too many bytes from i2c (%d)\n",
			    __func__, ret);
		    return -EIO;
	    }

	    if (copy_to_user(buf, tmp, ret)) {
		    pr_err("%s : failed to copy to user space\n", __func__);
		    return -EFAULT;
	    }
	}
	else
	{
		pr_err("%s: not supported operation_mode\n", __func__);
		return -EFAULT;
	}
#else /* CONFIG_SNFC */
	ret = i2c_master_recv(pn544_dev->client, tmp, count);
	mutex_unlock(&pn544_dev->read_mutex);

	if (ret < 0) {
		pr_err("%s: i2c_master_recv returned %d\n", __func__, ret);
		return ret;
	}
	if (ret > count) {
		pr_err("%s: received too many bytes from i2c (%d)\n",
			__func__, ret);
		return -EIO;
	}
	if (copy_to_user(buf, tmp, ret)) {
		pr_warning("%s : failed to copy to user space\n", __func__);
		return -EFAULT;
	}
#endif /* CONFIG_SNFC */
	return ret;

fail:
	mutex_unlock(&pn544_dev->read_mutex);
	return ret;
}

static ssize_t pn544_dev_write(struct file *filp, const char __user *buf,
		size_t count, loff_t *offset)
{
	struct pn544_dev  *pn544_dev;
	char tmp[MAX_BUFFER_SIZE];
	int ret;

	pn544_dev = filp->private_data;

#if defined(CONFIG_SNFC) && defined(NFC_DEBUG)
	pr_info("pn544 : + w\n");
#endif

	if (count > MAX_BUFFER_SIZE)
		count = MAX_BUFFER_SIZE;

	if (copy_from_user(tmp, buf, count)) {
		pr_err("%s : failed to copy from user space\n", __func__);
		return -EFAULT;
	}

	pr_debug("%s : writing %zu bytes.\n", __func__, count);
	/* Write data */
	ret = i2c_master_send(pn544_dev->client, tmp, count);

#if defined(CONFIG_SNFC) && defined(NFC_DEBUG)
	pr_info("pn544 : - w\n");
#endif

	if (ret != count) {
		pr_err("%s : i2c_master_send returned %d\n", __func__, ret);
		ret = -EIO;
	}

	return ret;
}

static int pn544_dev_open(struct inode *inode, struct file *filp)
{
	struct pn544_dev *pn544_dev = container_of(filp->private_data,
						struct pn544_dev,
						pn544_device);

	filp->private_data = pn544_dev;

	pr_debug("%s : %d,%d\n", __func__, imajor(inode), iminor(inode));

	return 0;
}

#ifdef KSK_MORE_KERNEL2636
static int pn544_dev_ioctl(struct file *filp,
		unsigned int cmd, unsigned long arg)
#else
static int pn544_dev_ioctl(struct inode *inode, struct file *filp,
		unsigned int cmd, unsigned long arg)
#endif
{
	struct pn544_dev *pn544_dev = filp->private_data;

	switch (cmd) {
	case PN544_SET_PWR:
#ifdef CONFIG_SNFC
		if (arg == 2) {
			/* power on with firmware download (requires hw reset)
			 */
#else
		if (arg > 1) {  /* enable with firmware download */
			pr_info("%s power on with firmware\n", __func__);
#endif

#ifdef CONFIG_SNFC			
			//if(!gpio_get_value(pn544_dev->ven_gpio))
			//	gpio_set_value(pn544_dev->ven_gpio, 1);
			pr_info("ven_gpio UP\n");	// TC4
			if(gpio_get_value(pn544_dev->ven_gpio))  // TC4
				gpio_set_value(pn544_dev->ven_gpio, 0);  // TC4
#endif				
			gpio_set_value(pn544_dev->firm_gpio, 1);

#ifdef CONFIG_SNFC
			operation_mode = DOWNLOAD_MODE;
			msleep(10);
			//gpio_set_value(pn544_dev->ven_gpio, 0);
			gpio_set_value(pn544_dev->ven_gpio, 1);  // TC4
			msleep(10);
#endif
			//gpio_set_value(pn544_dev->ven_gpio, 1);
			gpio_set_value(pn544_dev->ven_gpio, 0);	// TC4

#ifdef CONFIG_SNFC
			msleep(NFC_ON_DELAY);
#else
                   msleep(3);
#endif
		} else if (arg == 1) {
			/* power on */
			pr_info("%s power on\n", __func__);
			gpio_set_value(pn544_dev->firm_gpio, 0);
#ifdef CONFIG_SNFC
			operation_mode = NORMAL_MODE;
			
			//if(!gpio_get_value(pn544_dev->ven_gpio))
			//	gpio_set_value(pn544_dev->ven_gpio, 1);
			pr_info("ven_gpio UP\n");
			if(gpio_get_value(pn544_dev->ven_gpio))  // TC4
				gpio_set_value(pn544_dev->ven_gpio, 0);  // TC4
				
			msleep(NFC_ON_DELAY);
#else
                   msleep(3);
#endif
		} else  if (arg == 0) {
			/* power off */
			pr_info("%s power off\n", __func__);
			gpio_set_value(pn544_dev->firm_gpio, 0);
#ifdef CONFIG_SNFC
			operation_mode = NORMAL_MODE;
#endif
			//gpio_set_value(pn544_dev->ven_gpio, 0);
			gpio_set_value(pn544_dev->ven_gpio, 1);	// TC4
#ifdef CONFIG_SNFC
			msleep(10);
#else
                   msleep(7);
#endif
#ifdef CONFIG_SNFC
		} else {
			pr_err("%s bad arg %u\n", __func__, arg);
			return -EINVAL;
		}
#endif
		break;
	default:
		pr_err("%s bad ioctl %u\n", __func__, cmd);
		return -EINVAL;
	}

	return 0;
}

static const struct file_operations pn544_dev_fops = {
	.owner	= THIS_MODULE,
	.llseek	= no_llseek,
	.read	= pn544_dev_read,
	.write	= pn544_dev_write,
	.open	= pn544_dev_open,
#ifdef KSK_MORE_KERNEL2636
	.unlocked_ioctl  = pn544_dev_ioctl,
#else
	.ioctl  = pn544_dev_ioctl,
#endif
};

static int pn544_probe(struct i2c_client *client,
		const struct i2c_device_id *id)
{
	int ret;
	struct pn544_i2c_platform_data *platform_data;
	struct pn544_dev *pn544_dev;

	platform_data = client->dev.platform_data;

	if (platform_data == NULL) {
		pr_err("%s : nfc probe fail\n", __func__);
		return  -ENODEV;
	}

	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
		pr_err("%s : need I2C_FUNC_I2C\n", __func__);
		return  -ENODEV;
	}

	ret = gpio_request(platform_data->irq_gpio, "nfc_int");
	if (ret)
		return  -ENODEV;
	ret = gpio_request(platform_data->ven_gpio, "nfc_ven");
	if (ret)
		goto err_ven;
	ret = gpio_request(platform_data->firm_gpio, "nfc_firm");
	if (ret)
		goto err_firm;

	pn544_dev = kzalloc(sizeof(*pn544_dev), GFP_KERNEL);
	if (pn544_dev == NULL) {
		dev_err(&client->dev,
				"failed to allocate memory for module data\n");
		ret = -ENOMEM;
		goto err_exit;
	}

#if defined(CONFIG_SNFC) && defined(NFC_DEBUG)
	pr_info("%s : IRQ num %d\n", __func__, client->irq);
#endif

	pn544_dev->irq_gpio = platform_data->irq_gpio;
	pn544_dev->ven_gpio  = platform_data->ven_gpio;
	pn544_dev->firm_gpio  = platform_data->firm_gpio;
	pn544_dev->client   = client;

	/* init mutex and queues */
	init_waitqueue_head(&pn544_dev->read_wq);
	mutex_init(&pn544_dev->read_mutex);
	spin_lock_init(&pn544_dev->irq_enabled_lock);

	pn544_dev->pn544_device.minor = MISC_DYNAMIC_MINOR;
	pn544_dev->pn544_device.name = "pn544";
	pn544_dev->pn544_device.fops = &pn544_dev_fops;

	ret = misc_register(&pn544_dev->pn544_device);
	if (ret) {
		pr_err("%s : misc_register failed\n", __FILE__);
		goto err_misc_register;
	}

	/* request irq.  the irq is set whenever the chip has data available
	 * for reading.  it is cleared when all data has been read.
	 */
	pr_info("%s : requesting IRQ %d\n", __func__, client->irq);
	pn544_dev->irq_enabled = true;
#ifdef CONFIG_SNFC
	gpio_direction_input(pn544_dev->irq_gpio);
#endif
	ret = request_irq(client->irq, pn544_dev_irq_handler,
#ifdef CONFIG_SNFC
			  IRQF_TRIGGER_RISING, "pn544", pn544_dev);
#else
                     IRQF_TRIGGER_HIGH, client->name, pn544_dev);
#endif
	if (ret) {
		dev_err(&client->dev, "request_irq failed\n");
		goto err_request_irq_failed;
	}
	pn544_disable_irq(pn544_dev);
	i2c_set_clientdata(client, pn544_dev);

	return 0;

err_request_irq_failed:
	misc_deregister(&pn544_dev->pn544_device);
err_misc_register:
	mutex_destroy(&pn544_dev->read_mutex);
	kfree(pn544_dev);
err_exit:
	gpio_free(platform_data->firm_gpio);
err_firm:
	gpio_free(platform_data->ven_gpio);
err_ven:
	gpio_free(platform_data->irq_gpio);
	return ret;
}

static int pn544_remove(struct i2c_client *client)
{
	struct pn544_dev *pn544_dev;

	pn544_dev = i2c_get_clientdata(client);
	free_irq(client->irq, pn544_dev);
	misc_deregister(&pn544_dev->pn544_device);
	mutex_destroy(&pn544_dev->read_mutex);
	gpio_free(pn544_dev->irq_gpio);
	gpio_free(pn544_dev->ven_gpio);
	gpio_free(pn544_dev->firm_gpio);
	kfree(pn544_dev);

	return 0;
}

static const struct i2c_device_id pn544_id[] = {
	{ "pn544", 0 },
	{ }
};

static struct i2c_driver pn544_driver = {
	.id_table	= pn544_id,
	.probe		= pn544_probe,
	.remove		= pn544_remove,
	.driver		= {
		.owner	= THIS_MODULE,
		.name	= "pn544",
	},
};

/*
 * module load/unload record keeping
 */

static int __init pn544_dev_init(void)
{
	pr_info("Loading pn544 driver\n");
	return i2c_add_driver(&pn544_driver);
}
module_init(pn544_dev_init);

static void __exit pn544_dev_exit(void)
{
	pr_info("Unloading pn544 driver\n");
	i2c_del_driver(&pn544_driver);
}
module_exit(pn544_dev_exit);

MODULE_AUTHOR("Sylvain Fonteneau");
MODULE_DESCRIPTION("NFC PN544 driver");
MODULE_LICENSE("GPL");
